package com.example.a_java.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.a_java.dto.SummaryDTO;
import com.example.a_java.entity.LoanApply;
import com.example.a_java.entity.LoanOverdue;
import com.example.a_java.mapper.LoanOverdueMapper;
import com.example.a_java.service.CustInfoService;
import com.example.a_java.service.LoanApplyService;
import com.example.a_java.service.OverdueService;
import com.example.a_java.service.StatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class StatisticsServiceImpl implements StatisticsService {



    @Autowired
    private LoanApplyService loanApplyService;
    @Autowired
    private CustInfoService custInfoService;
    @Autowired
    private LoanOverdueMapper overdueService;

    @Override
    public SummaryDTO getSummaryStatistics() {
        SummaryDTO summary = new SummaryDTO();
        // TODO
        // 贷款总额
        summary.setTotalLoanAmount(loanApplyService.findAllByIsDeletedFalse().stream().mapToInt(LoanApply -> LoanApply.getLoanAmount().intValue()).sum());

        // 客户总数
        summary.setTotalClients(custInfoService.findAllByIsDeletedFalse().size());

        // 逾期笔数
        summary.setOverdueCount(Math.toIntExact(overdueService.selectCount(new QueryWrapper<>(new LoanOverdue()))));

        // 本月放款
         summary.setCurrentMonthDisbursement(loanApplyService.findAllByIsDeletedFalse().stream().mapToInt(LoanApply -> LoanApply.getLoanAmount().intValue()).sum());

        return summary;
    }
}